<?php
namespace app\merchant\controller;

class StoreInfo extends Init{
   function _initialize(){
       parent::_initialize();
   }
   
   public function index(){
     $store_id = session('store_id');
     $store_info = db('store')->where("store_id={$store_id}")->find();
     $this->assign('info',$store_info);
     return $this->fetch();
   }

   public function save(){
      if($this->request->isPost()){
          $store_id = session('store_id');
          $data = input('post.');
          if($data['phone']){
              $preg_phone='/^1[34578]\d{9}$/ims';
              if(!preg_match($preg_phone,$data['phone'])){
                  return json(array('code'=>201,'msg'=>'手机号码格式不正确'));
              }
          }
          if(empty($data['address'])){return json(array('code'=>201,'msg'=>'店铺地址不能为空'));}
          $save_info = db('store')->where("store_id={$store_id}")->update($data);
          if($save_info){
             return json(array('code'=>200,'msg'=>'门店信息修改成功'));
          }else{
             return json(array('code'=>201,'msg'=>'修改失败或信息无变化'));
          }
      }
   }

   public function account(){
         $store_id = session('store_id');
         $account_info = db('store')->where("store_id={$store_id}")->value('store_name');
         $this->assign('info',$account_info);
         return $this->fetch();
   }

   public function changePass(){
      if($this->request->isPost()){
         $store_id = session('store_id');
         $account_pass = db('store')->where("store_id={$store_id}")->value('store_pass');
         $data = input('post.');
         if($data['new_pass'] != $data['second_pass']){
            return json(array('code'=>201,'msg'=>'密码前后不一致'));
         }
         if($account_pass == strtolower(md5($data['new_pass']))){
            return json(array('code'=>201,'msg'=>'密码无变化'));
         }
         $change_pass = db('store')->where("store_id={$store_id}")->update(['store_pass'=>strtolower(md5($data['new_pass']))]);
         if($change_pass){
            session(null);
            return json(array('code'=>200,'msg'=>'密码修改成功,请重新登录'));
         }else{
            return json(array('code'=>200,'msg'=>'修改失败'));
         }
      }
   }
}